<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>数据库操作</title>
    <style>
        h1{ color:red;}
        .sql{
            color: blue;
            font-weight: bold;
            line-height: 2;
        }
        table{
            border: 3px double red;
            border-spacing: 0; /* 格子之间的间距 */
        }
        th, td{
            border-right: 1px dotted red;
            border-bottom: 1px dotted red;
            padding: 6px 10px;
        }
    </style>

</head>
<body>
<h1>命令执行结果</h1>
<hr>

<?php
$sqls=$_POST['sql']; //获取用户要执行的多条语句
$sqlArray=preg_split('@;\s*@', $sqls,flags:PREG_SPLIT_NO_EMPTY); //数组，包含每一条语句
$db=new PDO('mysql:host=localhost;dbname=db','root','12qwas');
$ps=$db->query($sqls);
$sqlIndex=0;
do{
    echo "<div class='sql'>",$sqlArray[$sqlIndex++] ,"</div>";
    if($cc=$ps->columnCount()){ //查询的操作, cc-字段的个数
        echo "<table><tr><th>序号</th>";
        for($i=0;$i<$cc;$i++){
            echo "<th>", $ps->getColumnMeta($i)['name'] ,"</th>";
        }
        echo "</tr>"; //表头行结束
        $xhIndex=0;
        foreach($ps as $record){ //循环完成每一行记录的显示
            echo "<tr><td>",++$xhIndex,"</td>";
            for($i=0;$i<$cc;$i++){
                echo "<td>", $record[$i]??$record[$ps->getColumnMeta($i)['name']]??'NULL' ,"</td>";
            }
            echo "</tr>"; //一行数据结束
        }
        echo "</table>";
    }else{ //增删改的操作
        echo "<div class='result'>受影响的行数：", $ps->rowCount() ,"</div>\n";
    }
}while($ps->nextRowset()); //继续处理下调语句的执行结果
?>
</body>
</html>